﻿
using Idte.Common.ArtifactModel;
using Idte.Common.Patterns;
using Idte.Common.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Idte.Application.Core.Navigation
{
    public static class MenuExtensions
    {

        public static Menu AsMenu<T>(this IComposite<T> composite, MenuType type)
        {
            Menu menu = CreateMenu(composite);
            menu.Type = type;
            return menu;
        }

        public static Menu AsMenu(this Artifact artifact, MenuType type)
        {
            Menu menu;
            if (artifact.Count == 0)
            {
                if (artifact.Value.GetType().IsAssignableFrom(typeof(IComposite<>)))
                {
                    menu = CreateMenu((IComposite)artifact.Value);
                }
                else
                    menu = new Menu();
            }
            else
            {
                menu = CreateMenu(artifact);
            }
            menu.Type = type;
            return menu;
        }

        public static ICollection<Menu> AsMenu(this ICollection<AppContext> contexts)
        {
            ICollection<Menu> menus = new List<Menu>();
            foreach( AppContext appCtx in contexts) {
                menus.Add(new Menu() { Title = appCtx.Title, Controller=appCtx.Controller, Action=appCtx.Action });
            }
            return menus;
        }

        //public static Menu AsMenu(this Process process, MenuType type)
        //{
        //    Menu menu = CreateMenu(process, false);
        //    menu.Type = type;
        //    return menu;
        //}

        //public static Menu AsMenu(this Process process, MenuType type, bool includeData)
        //{
        //    Menu menu = CreateMenu(process, includeData);
        //    menu.Type = type;
        //    return menu;
        //}

        //private static Menu CreateMenu(Process process, bool includeData)
        //{
        //    Menu menu = CreateMenu(process.Descriptor as ProcessDescriptor);
        //    return menu;
        //    //return null;
        //}

        private static Menu CreateMenu<T>(IComposite<T> composite)
        {
            Menu menu = new Menu();
            if (composite is INamed)
            {
                menu.Title = ((INamed)composite).Name;
            }
            else
            {
                menu.Title = composite.GetType().Name;
            }

            menu.Name = composite.Code;
            foreach (var item in composite.Children)
            {
                menu.Items.Add(CreateMenuItem(item as IComponent));
            }

            return menu;
        }

        private static MenuItem CreateMenuItem(IComponent step)
        {
            MenuItem item = new MenuItem();
            if (step is INamed)
            {
                item.Title = (step as INamed).Name;
            }
            else
            {
                item.Title = step.Code;
            }
            if (step is IComposite)
            {
                item.SubMenu = CreateMenu(step as IComposite);
            }

            return item;
        }
    }
}
